--Recipe.OnGiveXP = {}
require('NPCs/MainCreationMethods');
require("Items/Distributions");
require("Items/ProceduralDistributions");

playerdatatable = {}
playerdatatable[0] = { "MTModVersion", MTModVersion }
playerdatatable[1] = { "secondwinddisabled", false }
playerdatatable[2] = { "secondwindrecoveredfatigue", false }
playerdatatable[3] = { "secondwindcooldown", 0 }
playerdatatable[4] = { "bToadTraitDepressed", false }
playerdatatable[5] = { "indefatigablecooldown", 0 }
playerdatatable[6] = { "indefatigablecuredinfection", false }
playerdatatable[7] = { "indefatigabledisabled", false }
playerdatatable[8] = { "bindefatigable", false }
playerdatatable[9] = { "IndefatigableHasBeenDraggedDown", false }
playerdatatable[10] = { "bSatedDrink", true }
playerdatatable[11] = { "iHoursSinceDrink", 0 }
playerdatatable[12] = { "iTimesCannibal", 0 }
playerdatatable[13] = { "fPreviousHealthFromFoodTimer", 1000 }
playerdatatable[14] = { "bWasInfected", false }
playerdatatable[15] = { "iHardyEndurance", 0 }
playerdatatable[16] = { "iHardyMaxEndurance", 5 }
playerdatatable[17] = { "iHardyInterval", 1000 }
playerdatatable[18] = { "iWithdrawalCooldown", 24 }
playerdatatable[19] = { "iParanoiaCooldown", 10 }
playerdatatable[20] = { "SuperImmuneRecovery", 0 }
playerdatatable[21] = { "SuperImmuneActive", false }
playerdatatable[22] = { "SuperImmuneMinutesPassed", 0 }
playerdatatable[23] = { "SuperImmuneTextSaid", false }
playerdatatable[24] = { "SuperImmuneHealedOnce", false }
playerdatatable[25] = { "SuperImmuneMinutesWellFed", 0 }
playerdatatable[26] = { "SuperImmuneAbsoluteWellFedAmount", 0 }
playerdatatable[27] = { "SuperImmuneInfections", 0 }
playerdatatable[28] = { "SuperImmuneLethal", false }
playerdatatable[29] = { "MotionActive", false }
playerdatatable[30] = { "HasSlept", false }
playerdatatable[31] = { "FatigueWhenSleeping", 0 }
playerdatatable[32] = { "NeckHadPain", false }
playerdatatable[33] = { "ContainerTraitIllegal", false }
playerdatatable[34] = { "ContainerTraitPlayerCurrentPositionX", 0 }
playerdatatable[35] = { "ContainerTraitPlayerCurrentPositionY", 0 }
playerdatatable[36] = { "AlbinoTimeSpentOutside", 0 }
playerdatatable[37] = { "isMTAlcoholismInitialized", false }
playerdatatable[38] = { "iBouncercooldown", 0 }
playerdatatable[39] = { "bisInfected", false }
playerdatatable[40] = { "bisAlbinoOutside", false }
playerdatatable[41] = { "bToadTraitDepressed", false }
playerdatatable[42] = { "bWasJustSprinting", false }
playerdatatable[43] = { "InjuredBodyList", {} }
playerdatatable[44] = { "UnwaveringInjurySpeedChanged", false }
playerdatatable[45] = { "OldCalories", 810 }
playerdatatable[46] = { "IngenuitiveActivated", false }
playerdatatable[47] = { "EvasivePlayerInfected", false }
playerdatatable[48] = { "TraitInjuredBodyList", {} }
playerdatatable[49] = {"fLastHP", 0}
playerdatatable[50] = {"isSleeping", false}



function Recipe.OnCreate.DaGeJiaoXian(items, result, player)
    player:getInventory():AddItems("JingYe",1)
end

function Recipe.OnCreate.BaiNian(items, result, player)
    player:Say(" "..tostring(getText("IGUI_BaiNian")))
    processGeneralMessage(getText("IGUI_BaiNian2"))
end   

function Recipe.OnCreate.HuJiu(items, result, player)
    local Catch = ZombRand(10)
    if Catch == 0 then
        player:Say(" "..tostring(getText("IGUI_AA")))    end
        if Catch == 1 then
            player:Say(" "..tostring(getText("IGUI_BB")))
        end
            if Catch == 2 then
                player:Say(" "..tostring(getText("IGUI_CC")))
            end
            if Catch == 3 then
                player:Say(" "..tostring(getText("IGUI_DD")))
            end
            if Catch == 4 then
                player:Say(" "..tostring(getText("IGUI_EE")))
            end
            if Catch == 5 then
                player:Say(" "..tostring(getText("IGUI_FF")))
            end
            if Catch == 6 then 
                player:Say(" "..tostring(getText("IGUI_GG")))
            end
            if Catch == 7 then 
                player:Say(" "..tostring(getText("IGUI_HH")))
            end
            if Catch == 8 then 
                player:Say(" "..tostring(getText("IGUI_II")))
            end
            if Catch == 9 then 
                player:Say(" "..tostring(getText("IGUI_JJ")))
            end
    
end

--[[
function Recipe.OnCreate.coordinate(items, result, player)
    player:Say(" "..tostring(getText("IGUI_JJ")))
]]




function Recipe.OnCreate.TS_Maintenance(items, result, player)
    local XP = getSandboxOptions():getOptionByName("XP_Maintenance_CM"):getValue()
    player:getXp():AddXP(Perks.Maintenance, XP);  
end

function Recipe.OnCreate.TS_Mechanics(items, result, player)
    local XP = getSandboxOptions():getOptionByName("XP_Mechanics_CM"):getValue()
    player:getXp():AddXP(Perks.Mechanics, XP);
end

function Recipe.OnCreate.TS_Tailoring(items, result, player)
    local XP = getSandboxOptions():getOptionByName("XP_Tailoring_CM"):getValue()
    player:getXp():AddXP(Perks.Tailoring, XP);
end

function Recipe.OnCreate.TS_Nimble(items, result, player)
    local XP = getSandboxOptions():getOptionByName("XP_Nimble_CM"):getValue()
    player:getXp():AddXP(Perks.Nimble, XP);
end

function Recipe.OnCreate.TS_Doctor(items, result, player)
    local XP = getSandboxOptions():getOptionByName("XP_Doctor_CM"):getValue()
    player:getXp():AddXP(Perks.Doctor, XP);
end



--无限子弹
--[[
local function keepFill_CM()
    local playerObj = getPlayer()
    local gun = playerObj:getInventory():FindAndReturn("DoubleBarrelShotgun_CM")
    if gun then
        gun:setCurrentAmmoCount(gun:getMaxAmmo())
    end
end
Events.EveryOneMinute.Add(keepFill_CM)
]]



local function keepFill_CM()
    local playerObj = getPlayer()
    if not playerObj then return end
    local gun = playerObj:getPrimaryHandItem()
    if gun then
        if gun:getType() == "DoubleBarrelShotgun_CM" then
            gun:setCurrentAmmoCount(gun:getMaxAmmo())
        end
    end
end
Events.EveryOneMinute.Add(keepFill_CM)
--[[
local function Fill_CM(items, result, player) 
    result:setCurrentAmmoCount(result:getMaxAmmo())
end --装填
]]
--[[
local function delete_ShotgunShells_CM()
    local playerObj = getPlayer()
    if playerObj:getInventory():contains("ShotgunShells_CM") then
        while playerObj:getInventory():contains("ShotgunShells_CM") do
            playerObj:getInventory():Remove("ShotgunShells_CM")
        end
    end
end  --删霰弹
Events.EveryTenMinutes.Add(delete_ShotgunShells_CM)
]]


function Recipe.OnCreate.HongBao(items, result, player)
    local i = ZombRand(11)
    if i == 0 then
    player:getInventory():AddItems("BaiNianPic1",1)  
    end
    if i == 1 then
    player:getInventory():AddItems("BaiNianPic2",1)
    end
    if i == 2 then
        player:getInventory():AddItems("BaiNianPic3",1)
        end
        if i == 3 then
            player:getInventory():AddItems("BaiNianPic4",1)
            end
            if i == 4 then
                player:getInventory():AddItems("BaiNianPic5",1)
                end
                if i == 5 then
                    player:getInventory():AddItems("BaiNianPic6",1)
                    end
                    if i == 6 then
                        player:getInventory():AddItems("BaiNianPic7",1)
                        end
                        if i == 7 then
                            player:getInventory():AddItems("BaiNianPic8",1)
                            end
                            if i == 8 then
                                player:getInventory():AddItems("BaiNianPic9",1)
                                end
                                if i == 9 then
                                    player:getInventory():AddItems("BaiNianPic10",1)
                                    end
                                    if i == 10 then
                                        player:getInventory():AddItems("BaiNianPic11",1)
                                        end
end

function Recipe.OnCreate.TS_gamble(items, result, player)
    local i = ZombRand(100)
    if i <35 then
        player:Say(getText("IGUI_TS_gamble_falied"))
    elseif i<85 then
        player:Say(getText("IGUI_TS_gamble_2"))
        player:getInventory():AddItems("SuiPian",2)
    elseif i<92 then
        player:Say(getText("IGUI_TS_gamble_3"))
        player:getInventory():AddItems("SuiPian",4)
    else
        player:Say(getText("IGUI_TS_gamble_4"))
        player:getInventory():AddItems("SuiPian",8)
    end
end




function Recipe.OnCreate.deleteRandomTraits(items, result, player)
    local goodtra = {}
    local badtra = {}

    for i=0, TraitFactory.getTraits():size()-1 do
        
        local traita = TraitFactory.getTraits():get(i)
        
        if not player:getTraits():contains(traita:getType()) and traita:getCost() >= 0 then
            table.insert(goodtra, traita)
        end
        
        if player:getTraits():contains(traita:getType()) and traita:getCost() < 0 then
            table.insert(badtra, traita)
        end
    end

    local goodtraaa = goodtra[ZombRand(#goodtra) + 1]
    
    local badtraaa = badtra[ZombRand(#badtra) + 1]

    if #goodtra >= 0 and #badtra > 0 then
        player:getTraits():remove(badtraaa:getType())
        
        player:Say(getText("IGUI_TS_trait3") .. badtraaa:getLabel())
    end

    if #goodtra >= 0 and #badtra == 0 then
        player:Say(getText("IGUI_TS_trait1"))
    end

    -- player:getBodyDamage():setOverallBodyHealth(0.8 * player:getBodyDamage():getOverallBodyHealth())
    local aaaa = 1 - ( (1 - player:getStats():getHunger() )* 0.9 )
    local bbbb = 1 - ( (1 - player:getStats():getThirst() )* 0.9 )
    
    player:getStats():setHunger(aaaa)
    player:getStats():setThirst(bbbb)

    getPlayer(player):getNutrition():setWeight(getPlayer(player):getNutrition():getWeight() - 2)

    player:Say(getText("IGUI_TS_trait2"))
end


local SpeedFlag = false

local function bloodAmulet(playerobj)
    local playerObj = getPlayer()
    local player = getPlayer()
        
    if playerObj:getInventory():contains("bloodAmulet") then

        SpeedFlag = false

        caltimeav = 13
        getPlayer():setVariable("runspeed_1", 1.4)
        getPlayer():getBodyDamage():ReduceGeneralHealth(0.0085);
        getPlayer():getStats():setEndurance(getPlayer():getStats():getEndurance() + 0.0012);
        getPlayer():getStats():setDrunkenness(100); 
        getPlayer():getStats():setPanic(getPlayer():getStats():getPanic() + 120);
        --getPlayer():getStats():setBoredom(100);
        if not player:HasTrait("batteringram") then
            local playerdata = player:getModData();
            local Fitnesslvl = player:getPerkLevel(Perks.Fitness);
            if Fitnesslvl == 0 then
                Fitnesslvl = 1;
            end
            local endurancereduction = (10 / Fitnesslvl) * 0.01;
            local stats = player:getStats();
            local endurance = stats:getEndurance();
            local inTree = player:getCurrentSquare():Has(IsoObjectType.tree);
            if player:isSprinting() then
                local bodydamage = player:getBodyDamage();
                if bodydamage:getBodyPart(BodyPartType.UpperLeg_L):getFractureTime() > 1 or bodydamage:getBodyPart(BodyPartType.UpperLeg_R):getFractureTime() > 1 or bodydamage:getBodyPart(BodyPartType.LowerLeg_L):getFractureTime() > 1 or bodydamage:getBodyPart(BodyPartType.LowerLeg_R):getFractureTime() > 1 or bodydamage:getBodyPart(BodyPartType.Foot_L):getFractureTime() > 1 or bodydamage:getBodyPart(BodyPartType.Foot_R):getFractureTime() > 1 then
                    return ;
                end
                playerdata.bWasJustSprinting = true;
                local nearbyzombies = false;
                local enemies = player:getSpottedList();
                for i = 0, enemies:size() - 1 do
                    local enemy = enemies:get(i);
                    if enemy:isZombie() then
                        local distance = enemy:DistTo(player)
                        if distance <= 2 then
                            nearbyzombies = true;
                            break ;
                        end
                    end
                end
                if inTree == false and nearbyzombies == true then
                    player:setGhostMode(true);
                else
                    player:setGhostMode(false);
                end
                for i = 0, enemies:size() - 1 do
                    local enemy = enemies:get(i);
                    if enemy:isZombie() then
                        local distance = enemy:DistTo(player)
                        if distance <= 1.0 and enemy:isKnockedDown() == false then
                            enemy:setKnockedDown(true);
                            enemy:setStaggerBack(true);
                            enemy:setHitReaction("");
                            enemy:setPlayerAttackPosition("FRONT");
                            enemy:setHitForce(2.0);
                            enemy:reportEvent("wasHit");
                            stats:setEndurance(endurance - endurancereduction);
                        end
                    end
                end
                if internalTick >= 25 then
                    addSound(player, player:getX(), player:getY(), player:getZ(), 20, 25);
                end
            else
                if playerdata.bWasJustSprinting == true then
                    player:setGhostMode(false);
                    addSound(player, player:getX(), player:getY(), player:getZ(), 20, 25);
                    playerdata.bWasJustSprinting = false;
                end
            end
        end

    else
        if SpeedFlag == false then
        caltimeav = 13
        getPlayer():setVariable("runspeed_1", 1.0)
        SpeedFlag = true
        end
    end
end

Events.OnPlayerUpdate.Add(bloodAmulet)






function Recipe.OnCreate.kill(items, result, player)
    QNW_TS_ClientCommand.addZombie(player:getX(), player:getY(), 0, 1, 1)
end

function Recipe.OnCreate.MedicBag_C(items, result, player)
    player:getBodyDamage():AddGeneralHealth(100);
end

function Recipe.OnCreate.speed_C(items, result, player) 
    caltimeav = 13
    player:setVariable("runspeed_1", 1.5)
end

